package org.yangspring.samples.mvc.CtrlOracle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yangspring.SpringContextUtil;
import org.yangspring.samples.mvc.domainOracle.*;
import org.yangspring.samples.mvc.serviceOracle.*;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/22.
 */

@Controller
@RequestMapping("/oracle/sys")
public class OracleSysCtrl {

    private static final Logger logger = LoggerFactory.getLogger(OracleSysCtrl.class);

    @Autowired
//    @Resource(name = "tbspcService")
    private DBATablespacesService tbspcService;

    @Autowired
//    @Resource(name = "usersService")
    private DBAUsersService usersService;

    @Autowired
//    @Resource(name = "vdatafileService")
    private VdatafileService vdatafileService;

    @Autowired
//    @Resource(name = "dbaTablesService")
    private DBATablesService dbaTablesService;

    @Autowired
//    @Resource(name = "dbaViewService")
    private DBAViewsService dbaViewsService;


    @RequestMapping(value = "/")
    @ResponseBody
    public String oralceSysDef(){
        return "Oracle SYS Schema Default Page";
    }

    @RequestMapping(value = {"/tbspc", "/tbspc/list"})
    public String tpcList(Model model){

        logger.warn("\nOracle SYS.TABLESPACES Controller\n");

        model.addAttribute("tbspc001", new DBATablespaces());
        List<DBATablespaces> tbspcs = this.tbspcService.listTbspc();
        logger.warn("\nTablespace007 Quantity:\t" + tbspcs.size() + "\n");
        model.addAttribute("listTbspc007", tbspcs);
        return "listTbspc";
    }

    @RequestMapping(value = {"/user", "/user/list"})
    public String userList(Model model){

        logger.warn("\nOracle SYS.DBA_USERS Controller\n");

        model.addAttribute("user001", new DBAUsers());
        List<DBAUsers> users = this.usersService.listUser();
        logger.warn("\nUser007 Quantity:\t" + users.size() + "\n");
        model.addAttribute("listUser007", users);
        return "listUser";
    }

    @RequestMapping(value = {"/datafile", "/datafile/list"})
    public String datafileList(Model model){

        logger.warn("\nOracle SYS.V_$DATAFILE Controller\n");

        model.addAttribute("datafile001", new Vdatafile());
        List<Vdatafile> dfs = this.vdatafileService.listVdatafile();
        logger.warn("\nDatafile007 Quantity:\t" + dfs.size() + "\n");
        model.addAttribute("listDatafile008", dfs);
        return "listDatafile";
    }

    @RequestMapping(value = "/dbatablesall")
    public String dbaTablesList(Model model){
        logger.warn("\nOracle SYS.DBA_TABLES Controller\n");

        model.addAttribute("table001", new DBATables());
        List<DBATables> dbatables = this.dbaTablesService.listDbaTables();
        logger.warn("\nDbatables007 Quantity:\t" + dbatables.size() + "\n");
        model.addAttribute("listDbaTables008", dbatables);
        return "listDbaTables";
    }

    @RequestMapping(value = "/dbatables/{owner}")
    public String dbaTables4User(Model model, @PathVariable String owner){
        logger.warn("\nOracle SYS.DBA_TABLES for Owner Controller\n");

        model.addAttribute("table001", new DBATables());
        List<DBATables> dbatables = this.dbaTablesService.listDbaTables4Owner(owner);
        logger.warn("\nDbatables007 Quantity:\t" + dbatables.size() + "\n");
        model.addAttribute("listDbaTables008", dbatables);
        return "listDbaTables";
    }

    @RequestMapping(value = "/dbatables")
    public String dbaTables4Name(
            Model model,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "name2", required = false) String name2,
            @RequestParam(value = "owner", required = false) String owner
        ){
        logger.warn("\nOracle SYS.DBA_TABLES for Owner Controller with Filter\n");

        model.addAttribute("table001", new DBATables());
        List<DBATables> dbatables = null;
        if (name == null && owner == null) {
            dbatables = this.dbaTablesService.listDbaTables();
        } else if (owner != null){
            dbatables = this.dbaTablesService.listDbaTables4NameOwner(owner, (name == null) ? "" : name + ((name2 == null) ? "": "%" + name2));
        } else if (name != null){
            dbatables = this.dbaTablesService.listDbaTables4Name(name + ((name2 == null) ? "" : "%" + name2));
        }

        logger.warn("\nDbatables007 Quantity:\t" + dbatables.size() + "\n");
        model.addAttribute("listDbaTables008", dbatables);
        return "listDbaTables";
    }


    @RequestMapping(value = "/dbaviewsall")
    public String dbaViewsList(Model model){
        logger.warn("\nOracle SYS.DBA_VIEWS Controller\n");

        model.addAttribute("view001", new DBAViews());
        List<DBAViews> dbaviews = this.dbaViewsService.listDbaViews();
        logger.warn("\nDbaviews007 Quantity:\t" + dbaviews.size() + "\n");
        model.addAttribute("listDbaViews008", dbaviews);
        return "listDbaViews";
    }

//    @RequestMapping(value = "/dbaviews", params = "owner")
//    public String dbaViews4User(Model model, @RequestParam("owner") String owner){
//        logger.warn("\nOracle SYS.DBA_VIEWS for Owner Controller\n");
//
//        model.addAttribute("view001", new DBAViews());
//        List<DBAViews> dbaviews = this.dbaViewsService.listDbaViews4Owner(owner);
//        logger.warn("\nDbatviews007 Quantity:\t" + dbaviews.size() + "\n");
//        model.addAttribute("listDbaViews008", dbaviews);
//        return "listDbaViews";
//    }

    @RequestMapping(value = "/dbaviews")
    public String dbaViews4Name(
            HttpServletRequest request,
            Model model,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "name2", required = false) String name2,
            @RequestParam(value = "owner", required = false) String owner
        ){
        logger.warn("\nOracle SYS.DBA_VIEWS for Name Controller with Filter\n");

//        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
//        WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()).publishEvent(new ContentEvent("Now FrankieYang 's Blog Updating\n"));


        model.addAttribute("view001", new DBAViews());
        List<DBAViews> dbaviews = null;
        if (name == null && owner == null){
            dbaviews = this.dbaViewsService.listDbaViews();
        } else if (owner != null){
            dbaviews = this.dbaViewsService.listDbaViews4NameOwner(owner, (name == null) ? "" : name + ((name2 == null) ? "": "%" + name2));
        } else if (name != null) {
            dbaviews = this.dbaViewsService.listDbaViews4Name(name + ((name2 == null) ? "" : "%" + name2));
        }

        logger.warn("\nDbatviews007 Quantity:\t" + dbaviews.size() + "\n");
        model.addAttribute("listDbaViews008", dbaviews);
        return "listDbaViews";
    }


/*
    ContextLoader cntxLdr;
    ContextLoaderListener cntxLdrLsnr;
    FrameworkServlet frmwkSvlt;
    ClassPathXmlApplicationContext classPathXAC;
    BeanPostProcessor beanPostPrcs;
*/


/*
    BeanPostProcessor beanPostPrcs2;
    BeanFactoryPostProcessor beanFacPostPrcs2;
    PropertyPlaceholderConfigurer prptyPlhdrConfgr;
    InstantiationAwareBeanPostProcessor instAwrBeanPostPrcs;

    CommonAnnotationBeanPostProcessor commonAnnoBeanPostPrcs;
    InstantiationAwareBeanPostProcessor xxx;
    BeanDefinitionRegistry beanDefReg;
    BeanDefinitionRegistryPostProcessor beanDefRegPostPrcs;

*/

/*
    InitializingBean initBean;
    DisposableBean dispBean;
*/

    @PostConstruct
    public void setTbspcService11(){
    }
    @PreDestroy
    public void setUsersService22(){
    }


/*
    ApplicationContext appAC;
    AbstractApplicationContext absAC;
    ApplicationContext ac;
    AnnotationConfigApplicationContext annoCfgAppCntx;
*/


/*
    ApplicationEventMulticaster appEvtMltcst;
    SimpleApplicationEventMulticaster smpAEM;
    ApplicationListener appLsnr;
    ApplicationEvent appEvt;
*/


//    SmartLifecycle smartLC;

//    DispatcherServlet dispSvlt;


    public void funTest(){

        ApplicationContext appCntx = SpringContextUtil.getApplicationContext();

    }


}
